package com.hzm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzm.dao.ChapterDao;
import com.hzm.dao.VideoDao;
import com.hzm.entity.Chapter;
import com.hzm.entity.Video;
import com.hzm.service.ChapterService;
import com.hzm.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: qy137-java
 * @description:
 * @author: 皇子明
 * @create: 2021-10-23 09:52
 **/
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterDao, Chapter> implements ChapterService {
    @Resource
    private ChapterDao chapterDao;
    @Resource
    private VideoDao videoDao;
    @Override
    public Result findChapterByCoursId(String courseId) {
        List<Chapter> list =chapterDao.findChapterByCourseId(courseId);
        return new Result(2000,"查询成功",list);
    }

    @Override
    public Result findChapterById(String id) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        List<Chapter> chapters =chapterDao.selectList(wrapper);
        List<Video> videos = videoDao.selectList(null);
        List<Chapter> chapters1 = new ArrayList<>();
        for (Chapter chapter: chapters
             ) {
            Chapter chapter1 = new Chapter();
            chapter1.setTitle(chapter.getTitle());
            chapter1.setSort(chapter.getSort());
            chapter1.setId(chapter.getId());
            List<Video> videos1 = new ArrayList<>();
            for (Video video: videos
                 ) {
                if (video.getChapterId().equals(chapter.getId())){
                    videos1.add(video);
                    chapter1.setVideo(videos1);
                }
                chapters1.add(chapter1);
            }

        }
        for (Chapter chapter2: chapters1
             ) {
            if (chapter2.getVideo()==null){
                chapter2.setPeriodNum(0);
            }else {
                chapter2.setPeriodNum(chapter2.getVideo().size());
            }
        }
        return new Result(2000,"查询成功",chapters1);
    }

    @Override
    public Result insertChapter(Chapter chapter) {
        if (StringUtils.isEmpty(chapter.getId())){
            int insert = chapterDao.insert(chapter);
            if (insert>0){
                Result result = findChapterById(chapter.getCourseId());
                List<Chapter> chapters=(List<Chapter>) result.getResult();
                return new Result(2000,"新增章节成功",chapters);
            }else {
                return new Result(5000,"新增章节失败",null);
            }
        }else {
            int i = chapterDao.updateById(chapter);
            if (i>0){
                Result result = findChapterById(chapter.getCourseId());
                List<Chapter> chapters =(List<Chapter>)result.getResult();
                return new Result(2000,"修改章节成功",chapters);
            }else {
                return  new Result(5000,"修改章节失败",null);
            }
        }
    }

    @Override
    public Result deleteChapterWithCourseId(Chapter chapter) {
        int i =chapterDao.deleteById(chapter.getId());
        if (i>0){
            Result chapterById = findChapterById(chapter.getCourseId());
            List<Chapter> result =(List<Chapter>) chapterById.getResult();
            for (Chapter chapter1: result
                 ) {
                if (chapter1.getSort()>chapter.getSort()){
                    chapter1.setSort(chapter1.getSort()-1);
                    int i1=chapterDao.updateById(chapter1);
                    return new Result(2000,"删除章节成功",null);
                }
            }
        }
        return new Result(2000,"删除章节成功",null);
    }
}















